package com.oim.core.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oim.core.app.AppContext;
import com.oim.core.bean.GroupHead;
import com.oim.core.bean.UserData;
import com.oim.core.bean.UserHead;
import com.oim.core.business.box.HeadBox;
import com.oim.core.business.box.PersonalBox;
import com.oim.core.business.manage.ImageManage;
import com.oim.core.business.manage.ListManage;
import com.oim.core.common.action.BackAction;
import com.oim.core.common.action.BackInfo;
import com.oim.core.common.app.service.Service;
import com.oim.core.common.app.view.MainView;
import com.oim.core.common.component.file.FileHandleInfo;
import com.oim.core.common.component.file.FileInfo;
import com.oim.core.common.config.ConfigManage;
import com.oim.core.common.data.UserSaveData;
import com.oim.core.common.data.UserSaveDataBox;

/**
 * 描述：
 * 
 * @author 夏辉
 * @date 2014年3月31日 上午11:45:15 version 0.0.1
 */
public class HeadService extends Service {

	public HeadService(AppContext appContext) {
		super(appContext);
	}

	public void setPersonalHead(UserHead userHead) {
		if (null != userHead && UserHead.type_custom.equals(userHead.getType())) {
			BackAction<FileHandleInfo> backAction = new BackAction<FileHandleInfo>() {

				@Override
				public void back(BackInfo backInfo, FileHandleInfo info) {
					if (backInfo.isSuccess()) {
						if (info.isSuccess()) {
							FileInfo fi = info.getFileInfo();
							userHead.setAbsolutePath(fi.getAbsolutePath());
							MainView mainView = appContext.getSingleView(MainView.class);
							mainView.refreshPersonalHead();
							
							PersonalBox pb=appContext.getBox(PersonalBox.class);
							UserData ud=pb.getUserData();
							
							UserSaveDataBox usdb = (UserSaveDataBox) ConfigManage.get(UserSaveDataBox.path, UserSaveDataBox.class);
							UserSaveData usd = new UserSaveData();
							usd.setAccount(ud.getAccount());
							usd.setUserHead(userHead);
							usdb.put(ud.getAccount(), usd);
							ConfigManage.addOrUpdate(UserSaveDataBox.path, usdb);
						}
					}
				}
			};
			HeadBox headBox = this.appContext.getBox(HeadBox.class);
			headBox.putUserHead(userHead);
			PersonalBox pb = this.appContext.getBox(PersonalBox.class);
			pb.setUserHead(userHead);

			ImageManage im = this.appContext.getManage(ImageManage.class);
			im.downloadUserHead(userHead, backAction);
		}
	}

	public void setUserHeadList(List<UserHead> headList) {

		if (null != headList) {
			Map<String, UserHead> map = new HashMap<String, UserHead>();
			List<UserHead> list = new ArrayList<UserHead>();
			for (UserHead h : headList) {
				if (UserHead.type_custom.equals(h.getType())) {
					map.put(h.getHeadId(), h);
					list.add(h);
				}
			}

			BackAction<List<FileHandleInfo>> backAction = new BackAction<List<FileHandleInfo>>() {

				@Override
				public void back(BackInfo backInfo, List<FileHandleInfo> list) {
					if (backInfo.isSuccess()) {

						for (FileHandleInfo info : list) {
							if (info.isSuccess()) {
								FileInfo fi = info.getFileInfo();
								UserHead h = map.get(fi.getId());
								if (null != h) {
									h.setAbsolutePath(fi.getAbsolutePath());
								}
							}
						}
						ListManage listManage = appContext.getManage(ListManage.class);
						listManage.refreshUserHead();
					}
				}
			};
			HeadBox headBox = this.appContext.getBox(HeadBox.class);
			ImageManage im = this.appContext.getManage(ImageManage.class);
			ListManage lm = this.appContext.getManage(ListManage.class);
			headBox.setUserHeadList(headList);
			lm.setUserHeadList(headList);
			im.downloadUserHead(list, backAction);
		}
	}

	public void setGroupHeadList(List<GroupHead> headList) {
		if (null != headList) {
			Map<String, GroupHead> map = new HashMap<String, GroupHead>();
			List<GroupHead> list = new ArrayList<GroupHead>();
			for (GroupHead h : headList) {
				if (GroupHead.type_custom.equals(h.getType())) {
					map.put(h.getHeadId(), h);
					list.add(h);
				}
			}

			BackAction<List<FileHandleInfo>> backAction = new BackAction<List<FileHandleInfo>>() {

				@Override
				public void back(BackInfo backInfo, List<FileHandleInfo> list) {
					if (backInfo.isSuccess()) {

						for (FileHandleInfo info : list) {
							if (info.isSuccess()) {
								FileInfo fi = info.getFileInfo();
								GroupHead h = map.get(fi.getId());
								if (null != h) {
									h.setAbsolutePath(fi.getAbsolutePath());
								}
							}
						}
						ListManage listManage = appContext.getManage(ListManage.class);
						listManage.refreshGroupHead();
					}
				}
			};
			HeadBox headBox = this.appContext.getBox(HeadBox.class);
			ImageManage im = this.appContext.getManage(ImageManage.class);
			headBox.setGroupHeadList(headList);
			im.downloadGroupHeadList(list, backAction);
		}
	}
}
